
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Atienza-Home
 */
public class DistribFrame2 extends javax.swing.JFrame {

    /**
     * Creates new form InitMain
     */
    
    ArrayList<JToggleButton> baskets_ = new ArrayList<JToggleButton>();
    Basket[] bcontain;
    ButtonGroup basketgroup =new ButtonGroup();
    ArrayList<JPanel> fpanel;
    ImageIcon basket = new ImageIcon("resources\\basket.png");
    boolean isframeenable=false;
    
    ArrayList<JButton> fbutton1_1  = new ArrayList<JButton>();
    ArrayList<JButton> fbutton2_1  = new ArrayList<JButton>();
    ArrayList<JButton> fbutton3_1  = new ArrayList<JButton>();
    ArrayList<JPanel> fpanel_1 = new ArrayList<JPanel>();
    
    ArrayList<Fruit> arrfruits_;
    ArrayList<JButton> fbutton1_;
    ArrayList<JButton> fbutton2_;
    ArrayList<JButton> fbutton3_ ;
    ArrayList<JPanel> fpanel_;
    
    public DistribFrame2(ArrayList<Fruit> arrfruits, ArrayList<JButton> fbutton1, ArrayList<JButton> fbutton2, ArrayList<JButton> fbutton3, ArrayList<JButton> baskets, ArrayList<JPanel> fpanel) {
        setVisible(true);
        setResizable(false);
        
        isframeenable=true;
        arrfruits_ = arrfruits;
        fbutton1_ = fbutton1;
        fbutton2_ = fbutton2;
        fbutton3_ = fbutton3;
        fpanel_ = fpanel;
        this.fpanel=fpanel;
        bcontain = new Basket[baskets.size()];
        
        initComponents();
        
        for(int i=0;i<baskets.size();i++){
            bcontain[i] = new Basket();
            
            JToggleButton button = new JToggleButton(basket);
            if(baskets.get(i).isVisible()==false)
                button.setVisible(false);
            this.baskets_.add(button);
            basketgroup.add(this.baskets_.get(i));
            
            jPanel3.add(fpanel.get(i));
            jPanel5.add(this.baskets_.get(i));
            
            
            fbutton2.get(i).setEnabled(false);
            
            
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(3,1));
            JButton button2 = new JButton();
            fbutton1_1.add(button2);
            panel.add(button2);
            button2 = new JButton(arrfruits.get(i).getName());
            button2.setEnabled(false);
            fbutton2_1.add(button2);
            panel.add(button2);
            button2 = new JButton("0");
            button2.setEnabled(false);
            fbutton3_1.add(button2);
            panel.add(button2);
            fpanel_1.add(panel);
            fpanel_1.get(i).setVisible(false);
            jPanel6.add(fpanel_1.get(i));
            jScrollPane5.setViewportView(jPanel6);
            
                           
        }
        
        
        for(int i=0;i<this.baskets_.size();i++){
            final int t=i;
            this.baskets_.get(i).addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    for(int j=0;j<fpanel_1.size();j++){
                       
                        fbutton3_1.get(j).setText(String.valueOf(bcontain[t].quantity[j]));
                        if(bcontain[t].quantity[j]!=0)
                            fpanel_1.get(j).setVisible(true);
                        else
                            fpanel_1.get(j).setVisible(false);
                        
                    }
                
                }           
            
            });
            
        }
        
        for(int i=0;i<fbutton3_.size();i++){
            final int t=i;
            final ArrayList<JToggleButton> thisbaskets = this.baskets_;
            fbutton3_.get(i).addActionListener(new ActionListener(){
               
                public void actionPerformed(ActionEvent e) {
                    for(int j=0;j<thisbaskets.size();j++){
                        if(thisbaskets.get(j).isSelected()==true){
                           
                           fbutton3_.get(t).setText(String.valueOf(Integer.parseInt(fbutton3_.get(t).getText())-1));
                           
                           if(fbutton3_.get(t).getText().equals("0"))
                               fpanel_.get(t).setVisible(false);
                                                      
                           fpanel_1.get(t).setVisible(true);
                           bcontain[j].quantity[t]++;
                           fbutton3_1.get(t).setText(String.valueOf(bcontain[j].quantity[t]));
                            
                        }
                        
                    }
                }
                              
            });
        }
        
        
        for(int i=0;i<fbutton1_1.size();i++){
            final int t=i;
            final ArrayList<JToggleButton> thisbaskets = this.baskets_;
            fbutton1_1.get(i).addActionListener(new ActionListener(){
               
                public void actionPerformed(ActionEvent e) {
                    for(int j=0;j<thisbaskets.size();j++){
                        if(thisbaskets.get(j).isSelected()==true){
                           fbutton3_.get(t).setText(String.valueOf(Integer.parseInt(fbutton3_.get(t).getText())+1));
                           
                           
                           
                           if(fbutton3_.get(t).getText().equals("0")==false)
                               fpanel_.get(t).setVisible(true);
                                                      
                           fpanel_1.get(t).setVisible(true);
                           bcontain[j].quantity[t]--;
                           fbutton3_1.get(t).setText(String.valueOf(bcontain[j].quantity[t]));
                           
                           if(bcontain[j].quantity[t]==0)
                               fpanel_1.get(t).setVisible(false);
                            
                        }
                        
                    }
                }
                              
            });
        }
        
        jButton2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                int fruitcount=0;boolean isfruitinbakset=true;
                
                for(int i=0;i<arrfruits_.size();i++){
                    fruitcount+=Integer.parseInt(fbutton3_.get(i).getText());
                }
                for(int i=0;i<bcontain.length;i++){
                    int temp=0;
                    for(int j=0;j<bcontain[i].quantity.length;j++)
                        temp+=bcontain[i].quantity[j];
                    if(temp==0 && baskets_.get(i).isVisible()==true){
                        isfruitinbakset=false;
                        break;
                    }
                    
                }
                
                if(fruitcount!=0)
                    JOptionPane.showMessageDialog(null,"All fruits not distributed!");
                else if(isfruitinbakset==false)
                    JOptionPane.showMessageDialog(null,"There is a basket that has no fruit/s in it!");
                else{
                    new PickBasketFrame2(arrfruits_,fbutton1_,fbutton2_,fbutton3_, fpanel_,baskets_,bcontain,basketgroup );
                    setVisible(false);
                    
                }
                
                
            }
        
        });
        
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jPanel6 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

       

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel3.setPreferredSize(new java.awt.Dimension(635, 97));

        jPanel3.setLayout(new GridLayout(1,10));
        jScrollPane2.setBorder(null);
        jScrollPane2.setViewportView(jPanel3);

        jPanel5.setPreferredSize(new java.awt.Dimension(635, 97));

        jPanel5.setLayout(new GridLayout(1,10));
        jScrollPane4.setBorder(null);
        jScrollPane4.setViewportView(jPanel5);

        jLabel3.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        int tint=0;String tstring= new String("");
        for(int i=0;i<arrfruits_.size();i++){
            if(tint<arrfruits_.get(i).getQuantity()){
                tstring = new String(arrfruits_.get(i).getName());
                tint = arrfruits_.get(i).getQuantity();
            }
            
            
        }
        
        
        jLabel3.setText("Fruits Available (Max is "+tstring+" with "+tint+" pieces.)");

        jLabel4.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabel4.setText("Baskets");

        jLabel5.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabel5.setText("Fruits Inside Basket");

        jPanel6.setPreferredSize(new java.awt.Dimension(635, 97));

        jPanel6.setLayout(new GridLayout(1,10));
        jScrollPane5.setBorder(null);
        jScrollPane5.setViewportView(jPanel6);

        jButton2.setText("Proceed>>");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addComponent(jScrollPane4)
            .addComponent(jScrollPane5)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    
    // Variables declaration - do not modify
    
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    // End of variables declaration
}
